package Decorator;

import java.awt.Color;
import java.awt.Graphics;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

import pyrachnid.model.ground_model.Cell;
import pyrachnid.model.robot_model.Robot;
import pyrachnid.model.scenar_model.Scenario;
import pyrachnid.view.dnd.ShortTransferHandler;



public class Panneau extends JPanel {
	private static final long serialVersionUID = 1L;
	private Item decor;
	private MapBase _mapBase;
	private Timer _timer;
	private Hashtable <Robot, RobotDecorator> _listeRobots;
	private Hashtable <Cell, FireDecorator> _listeFires;
	private boolean _isStated;
	
	public Panneau(){
		super();
        
        // Drag'n'Drop
        this.setTransferHandler(new ShortTransferHandler());
        
        _mapBase = new MapBase(this);
		this.decor = _mapBase;
		_listeRobots = new  Hashtable <Robot, RobotDecorator>();
		_listeFires = new  Hashtable <Cell, FireDecorator>();
		
		// Pause when init
		_isStated = false;
		

		_timer = new Timer();
		_timer.schedule (new MyTimer(this), 0, 600/60);
	}     
	

    @Override
    public void paintComponent(Graphics g) {
        //On d�cide d'une couleur de fond pour notre rectangle
        g.setColor(Color.GRAY);
        
        //On dessine le background
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
    	if(_mapBase.imageIsLoad())
    		this.decor.paintComponent(g);
    }
    
    public void start() {

		for(RobotDecorator robotD : _listeRobots.values())
    	{
    		robotD.startAnnimation();
    	}
		for(FireDecorator fireD : _listeFires.values())
    	{
			fireD.startAnnimation();
    	}
		
		_isStated = true;
    }
    
    public void stop(){
    	for(RobotDecorator robotD : _listeRobots.values())
    	{
    		robotD.stopAnnimation();
    	}
		for(FireDecorator fireD : _listeFires.values())
    	{
			fireD.stopAnnimation();
    	}
		
		_isStated = false;
    }

	public void loadImage(Scenario p_scenario) {
		try {
			_mapBase.loadImage(p_scenario);
			
			for(Robot robot : p_scenario.getRobotList())
				addRobotImage(robot);
			
			for(Cell cell : p_scenario.getMap().getCells()){
				if(cell.getIsInFire()){
					addFireImage(cell);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void removeMap(){
		_mapBase.removeImage();
		_mapBase = new MapBase(this);
		decor = _mapBase;
	}


	public void scaleImage() {
		_mapBase.scaleImage();
	}

	
	
    /** Add / Remove image **/
	
	public void addFireImage(Cell p_cell) {
		_listeFires.put(p_cell, new FireDecorator(this, this.decor, p_cell ,_mapBase));
		
		if(_isStated) _listeFires.get(p_cell).startAnnimation();
		
		this.decor = _listeFires.get(p_cell);
	}
	
	public void removeFireImage(Cell cellFire) {
		_listeFires.remove(cellFire);
		resetPanneau();
	}

	public void addRobotImage(Robot p_robot) {
		_listeRobots.put(p_robot, new RobotDecorator(this, this.decor, p_robot ,_mapBase));

		if(_isStated) _listeRobots.get(p_robot).startAnnimation();
		
		this.decor = _listeRobots.get(p_robot);
	}
	
	public void removeRobotImage(Robot p_robot){
		_listeRobots.remove(p_robot);
		resetPanneau();	
	}
	
	public void addNewElement(short p_typeElement) {
		_mapBase.addNewElement(p_typeElement);
	}
	
	/** Tool **/
	
	private void resetPanneau()
	{
		this.decor = _mapBase;

    	for(RobotDecorator robotD : _listeRobots.values())
		{
    		robotD.setItem(this.decor);
			this.decor = robotD;
		}

    	for(FireDecorator fireD : _listeFires.values())
		{
    		fireD.setItem(this.decor);
			this.decor = fireD;
		}
	}

	// Timer use to launch a update at the manager 
	public class MyTimer extends TimerTask {
		Panneau _panneau;
		
		public MyTimer(Panneau p_panneau) {
			super();
			_panneau = p_panneau;
		}
		
		@Override
		public void run() {
			try {
				_panneau.repaint();
			} catch (Exception e) {
				cancel();
	            e.printStackTrace();
			}
		}
	}
}
